﻿using System;
using System.Data;

using Uber.Storage;
using Uber.HabboHotel.GameClients;
using Uber.Messages;

using Uber.Collections;

namespace Uber.HabboHotel.Catalogs
{
    class VoucherHandler
    {
        public SafeDictionary<uint, Voucher> Vouchers;

        public VoucherHandler()
        {
            this.Vouchers = new SafeDictionary<uint, Voucher>();
        }

        public void InitVouchers()
        {
            this.Vouchers = null;
            this.Vouchers = new SafeDictionary<uint, Voucher>();

            DataTable Data = null;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                Data = dbClient.ReadDataTable("SELECT * FROM credit_vouchers ORDER BY id");
            }

            if (Data == null)
            {
                return;
            }

            foreach (DataRow Row in Data.Rows)
            {
                this.Vouchers.Add((uint)Row["id"], new Voucher((uint)Row["id"], (string)Row["code"], (int)Row["value"]));
            }
        }

        public Voucher GetVoucher(string Code)
        {
            foreach(Voucher mV in this.Vouchers.Values)
            {
                if (mV.Code == Code)
                {
                    return mV;
                }
            }

            return null;
        }

        public Boolean IsValidCode(string Code)
        {
            return (this.GetVoucher(Code) != null);
        }

        public int GetVoucherValue(string Code)
        {
            Voucher mV = null;

            if ((mV = GetVoucher(Code)) == null)
            {
                return 0;
            }

            if (mV.Value < 0)
            {
                return 0;
            }

            return mV.Value;
        }

        public void TryDeleteVoucher(string Code)
        {
            Voucher mV = null;

            if ((mV = GetVoucher(Code)) == null)
            {
                return;
            }

            this.Vouchers.Remove(mV.Id);

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("code", Code);
                dbClient.ExecuteQuery("DELETE FROM credit_vouchers WHERE code = @code LIMIT 1");
            }
        }

        public void TryRedeemVoucher(GameClient Session, string Code)
        {
            if (!IsValidCode(Code))
            {
                ServerMessage Error = new ServerMessage(213);
                Error.AppendRawInt32(1);
                Session.SendMessage(Error);
                return;
            }

            int Value = GetVoucherValue(Code);

            TryDeleteVoucher(Code);

            if (Value <= 0)
            {
                return;
            }

            if (Value > 0)
            {
                Session.GetHabbo().Credits += Value;
                Session.GetHabbo().UpdateCreditsBalance(true);
            }

            Session.SendMessage(new ServerMessage(212));
        }
    }

    class Voucher
    {
        public readonly uint Id;

        public string Code;

        public int Value;

        public Voucher(uint mId, string mCode, int mValue)
        {
            this.Id = mId;
            this.Code = mCode;
            this.Value = mValue;
        }
    }
}
